package com.gxa.eshop.user.controller;

import com.gxa.eshop.common.Result;
import com.gxa.eshop.exception.BusinessException;
import com.gxa.eshop.pojo.User;
import com.gxa.eshop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author bill
 * @date 2022/1/20 10:20
 */
@RequestMapping("/user")
@Controller
@ResponseBody
@Api(value = "登录相关接口文档")
public class UserController {


    @Reference
    private UserService userService;

    @RequestMapping(value = "/login")
    @ApiOperation(value = "登录",notes = "登录",httpMethod = "POST")
    public Result sendLoginSms(@RequestParam String phone, @RequestParam String code) throws BusinessException {
        if(!phone.matches("^[1](([3][0-9])|([4][5,7,9])|([5][0-9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$")){
            return new Result(5003,"手机号码格式不合法");
        }
        Map<String, String> result = userService.login(phone, code);
        return new Result(result);
    }


    @RequestMapping(value = "/query")
    @ApiOperation(value = "查询登录信息",notes = "查询登录信息",httpMethod = "POST")
    public Result query(@RequestParam String token) throws BusinessException {
        User user = userService.queryUserByToken(token);
        return new Result(user);
    }


}

